package com.example.wxy.bbtapp.DeviceService;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;

import com.example.wxy.bbtapp.Common.BitmapCompress;
import com.example.wxy.bbtapp.Startup.AppSettings;
import com.example.wxy.bbtapp.Models.CourseRecord.CourseRecord;
import com.example.wxy.bbtapp.Models.CourseRecord.ScreenshotRecord;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by yons on 2017/9/5.
 * 截屏
 */

public class ScreenShotService {

    public static String takeScreenShot() {

        String filename = java.util.UUID.randomUUID().toString();

        String mSavedPath = Environment.getExternalStorageDirectory() + File.separator + "ClassPic";

        File fileFolder = new File(mSavedPath);
        if (!fileFolder.exists()) {
            fileFolder.mkdirs();
        }

        mSavedPath +=  File.separator+ filename + ".png";

        try {
            Runtime.getRuntime().exec("screencap -p " + mSavedPath);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return filename + ".png";
    }

    public static void Shot(CourseRecord Current_CourseRecord,final Context c)
    {
        String mSavedPath = Environment.getExternalStorageDirectory() + File.separator + "ClassPic";
        // 截屏
        final String fileName = ScreenShotService.takeScreenShot();
        final String FilePath = mSavedPath + File.separator + fileName;
        ScreenshotRecord sr = new ScreenshotRecord();
        sr.CourseRecordID = Current_CourseRecord.getID();
        sr.FileName = fileName;
        sr.FilePath = FilePath;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        sr.ScreenTime = df.format(new Date());
        Current_CourseRecord.getScreenRecords().add(sr);

        Log.d(AppSettings.DebugTag, "takeScreenShot");

        int delay = 3000;
        new Handler().postDelayed(new Runnable() {
            public void run() {

                try
                {
                    BitmapCompress.compressPicture(FilePath, FilePath);

                }catch (Exception e)
                {
                    Log.d(AppSettings.DebugTag, "compressPicture error");
                }
                // 压缩图片

            }
        }, delay);
    }

    public static void ShotTest(CourseRecord Current_CourseRecord,final Context c)
    {
        ScreenshotRecord sr = new ScreenshotRecord();
        String mSavedPath = Environment.getExternalStorageDirectory() + File.separator + "ClassPic";
        int delay = 3000;
        for(int i=0;i<12; i++)
        {
            sr = new ScreenshotRecord();
            sr.CourseRecordID = Current_CourseRecord.getID();
            final String FilePath = mSavedPath + File.separator + i + ".jpg";
            sr.FilePath = FilePath;
            sr.FileName = i+".jpg";
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            sr.ScreenTime = df.format(new Date());
            Current_CourseRecord.getScreenRecords().add(sr);
            BitmapCompress.compressPicture(FilePath, FilePath);
//            new Handler().postDelayed(new Runnable() {
//                public void run() {
//
//                    try
//                    {
//                        BitmapCompress.compressPicture(FilePath, FilePath);
//                        Log.d(AppSettings.DebugTag, "compressPicture");
//                        Toast.makeText(c, "BitmapCompressed", Toast.LENGTH_LONG).show();
//                    }catch (Exception e)
//                    {
//                        Toast.makeText(c, "BitmapCompresse error", Toast.LENGTH_LONG).show();
//                    }
//
//                }
//            }, delay);
        }
    }


}